namespace Prometheus.Binding.Validation.Rules.Simple
{
    public class proIsUnsignedDecimal : proValidationRuleBase
    {
        private bool _canBeZero;

        public proIsUnsignedDecimal()
        {
            CanBeZero = false;
        }

        public bool CanBeZero
        {
            get { return _canBeZero; }
            set
            {
                _canBeZero = value;
                ErrorText = value ? "Value must not be negative" : "Value must be positive";
            }
        }

        public override void Validate(object value, object dataContext)
        {
            if (value == null) return;


            decimal dec;
            if (decimal.TryParse(value.ToString(), out dec))
            {
                if (dec < 0) ThrowInvalid();

                if (dec == 0 && !CanBeZero) ThrowInvalid();
            }
            else
            {
                ThrowInvalid();
            }
        }
    }
}